<template>
    <el-card shadow="hover" style="width:80%;">
      <!--1. 分页查询条件定义-->
      <el-form :inline="true" :model="student" class="demo-form-inline" align="left">
        <el-form-item >
          <el-input v-model="student.sname" placeholder="输入学生姓名"></el-input>
        </el-form-item>
        <el-form-item >
          <el-input v-model="student.addr" placeholder="输入学生住址"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="student.cid" placeholder="所在班级">
            <el-option label="所有班级" value="0"></el-option>
            <el-option :label="c.cname" :value="c.cid" v-for="c in classes" :key="c.cid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
      <!--2. 分页数据显示-->
      <el-table
              align="center"
              :data="students">
        <el-table-column
                label="学号"
                prop="sid"
                align="center"
                width="180">
        </el-table-column>
        <el-table-column
                label="姓名"
                prop="sname"
                align="center">
        </el-table-column>
        <el-table-column
                label="性别"
                prop="sex"
                align="center">
        </el-table-column>
        <el-table-column
                label="年龄"
                prop="age"
                align="center">
        </el-table-column>
        <el-table-column
                label="住址"
                prop="addr"
                align="center">
        </el-table-column>
        <el-table-column
                label="所在班级"
                prop="cname"
                align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
                    size="mini"
                    @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="page"
              :page-sizes="[5, 10, 15, 20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
      </el-pagination>
    </el-card>

</template>

<style>
  .el-card{
    text-align: center;
    border:1px solid red;
    margin: 0 auto;
  }
  .el-pagination{
    text-align: right;
    margin-top: 20px;
  }
</style>

<script>
  import {findAll,findByPage,search} from "@/api/students";
  import {findAllClasses} from "@/api/classes";

  export default {
    data() {
      return {
        students: [],
        page: 1,
        pageSize: 5,
        total:100,
        student:{},
        classes:[],

      }
    },
    created() {
      //1. 查询所有学生
      // this.findAllStudents()
      //2. 分页查询学生
      // this.findByPage()
      //3. 查询所有班级
      this.findClasses()
      //4. 条件查询带分页
      this.search()
    },
    methods: {
      //1. 查询所有学生
      findAllStudents(){
        findAll().then(({data})=>{
          console.log("data:",data)
          this.students = data.data.list
        })
      },
      //2. 分页查询学生
      findByPage(){
        findByPage(this.page,this.pageSize).then(({data})=>{
          console.log("data:",data)
          this.students = data.data.pr.rows
          this.total = data.data.pr.total
        })
      },
      //3. 查询所有班级
      findClasses(){
        findAllClasses().then(({data})=>{
          this.classes = data.data.list;
        })
      },
      //4. 条件查询带分页
      search(){
        search(this.page,this.pageSize,this.student).then(({data})=>{
          this.students = data.data.pr.rows
          this.total = data.data.pr.total
        })
      },
      handleEdit(index, row) {
        console.log(index, row);
      },
      handleDelete(index, row) {
        console.log(index, row);
      },
      //页大小改变时触发
      handleSizeChange(val) {
        this.pageSize = val
        // this.findByPage()
        this.search()
        console.log(`每页 ${val} 条`);
      },
      //当前页改变时触发
      handleCurrentChange(val) {
        this.page = val;
        // this.findByPage()
        this.search()
        console.log(`当前页: ${val}`);
      }
    }
  }
</script>
